package com.lyf.boot.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.lyf.boot.modules.system.service.IFileService;
import com.lyf.boot.modules.system.service.IOssConfigService;
import com.lyf.boot.utils.file.FileUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.upload.UploadPretreatment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Luyufan
 * @date 2023/8/15 14:38
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class FileServiceImpl implements IFileService {

    private final FileStorageService fileStorageService;

    private final IOssConfigService ossConfigService;

    @Override
    public FileInfo upload(MultipartFile file, String path, String platform) {
        UploadPretreatment pretreatment = fileStorageService.of(file);
        if(StrUtil.isNotBlank(path)){
            pretreatment.setPath(path);
        }
        if(StrUtil.isBlank(platform)){
            //获取默认的
            platform = ossConfigService.getDefaultPlatform();
        }
        String fileMd5 = FileUtil.getFileMd5(file);
        log.debug("文件md5:{}", fileMd5);
        pretreatment.setPlatform(platform);
        return pretreatment.setProgressMonitor((progressSize, allSize) -> {
            log.debug("上传进度:{}", progressSize);
            double progress = (double) progressSize /allSize;
            //SSEServer.sendMessage(fileMd5, progress * 100);
        }).upload();
    }
}
